package com.hzvtc.bosswang.manageuser.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzvtc.bosswang.managecommon.domain.AjaxResult;
import com.hzvtc.bosswang.manageuser.domain.User;
import com.hzvtc.bosswang.manageuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user-manager")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/select-user/{id}")
    public User selectUserByStudentId(@PathVariable Long id){
        return userService.selectUserByStudentId(id);


    }
    @PostMapping("/list-user")
    public AjaxResult listUser(@RequestBody User user){
        Page<User> page=new Page<>();
        page.setCurrent(1);
        page.setSize(10);
        User userCond=new User();
        userCond.setUserName(user.getUserName());
        userCond.setRealName(user.getRealName());
        return new AjaxResult(200,"查询成功",userService.selectUserList(page,userCond));
    }
    @GetMapping("/view-user/{id}")
    public AjaxResult selectUserById(@PathVariable Long id){
        User user = userService.selectUserById(id);
        if (user!=null){
            user.setPwdSalt(null);
            user.setPwd(null);
            user.setPwdErrorCount(0);
            user.setPwdValidDate(null);
            return new AjaxResult(200,"查询成功",user);
        }else {
            return new AjaxResult(500,"查询失败",null);
        }
    }

    @PostMapping("/add-user")
    public AjaxResult addUser(@RequestBody User user){
        Integer integer = userService.addUser(user);
        if (integer>0){
            return new AjaxResult(200,"添加成功",null);
        }
        return new AjaxResult(500,"添加失败，用户名已存在",null);
    }
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){

        Boolean isLogin = userService.isLogin(user.getUserName());
        if (isLogin){
            return new AjaxResult(200,"用户"+user.getUserName()+"已登录",null);
        }
        Integer pwdErrorCount=userService.isValidate(user.getUserName());
        if (pwdErrorCount>=3){
            return new AjaxResult(500,"用户"+user.getUserName()+"因多次输入密码错误已被禁用，请联系管理员",null);
        }
        if (pwdErrorCount==-1){
            return new AjaxResult(500,"用户"+user.getUserName()+"不存在",null);
        }
        int remainTimes=3-pwdErrorCount;
        Boolean login = userService.login(user);
        if (login){
            return new AjaxResult(200,"登陆成功",null);
        }else {
            return new AjaxResult(500,"用户名，密码或用户类型错误，您还可以重试"+remainTimes+"次",null);
        }
    }

    @PostMapping("/edit-user")
    public AjaxResult editUser(@RequestBody User user){
        int res=userService.editUser(user);
        if (res>0){
            return new AjaxResult(200,"修改成功！",null);
        }else {
            return new AjaxResult(500,"修改失败！",null);
        }
    }

    @DeleteMapping("/delete-user/{id}")
    public AjaxResult deleteUser(@PathVariable Long id){
        int res=userService.deleteUser(id);
        if (res>0){
            return new AjaxResult(200,"删除成功！",null);
        }else {
            return new AjaxResult(500,"删除失败！",null);
        }
    }
}
